var x = 0;
var homeInfo = null;
var iosInfo = null;
// var pageNo = 1;
var Popularize = {
    init: function () {
        this.getData(1, 1);
        // this.getData(2,1);
    },
    eventLisenter: function () {
        var that = this;
        $(".intenthtml").click(function () {
            window.location.href = $(this).attr("href");
        });
        $(".basehtml").scroll(function (e) {
            var scrollHeight = $(".basehtml")[0].scrollHeight;
            var height = $(window).height();
            var scrollTop = $(".basehtml").scrollTop();
            if ($("#home").hasClass("active")) {
                if (homeInfo.length==10) {
                    if (scrollTop == scrollHeight - height) {
                        that.getData(1, ++homeInfo.pageNo);
                        // $("#home .p").text("正在加载");
                    }
                } else{
                    $("#home .p").text("-没有更多-");
                }
            } else {
                if (iosInfo.length==10) {
                    if (scrollTop == scrollHeight - height) {
                        that.getData(2, ++iosInfo.pageNo);
                        // $("#ios .p").text("正在加载");
                    }
                } else{
                    $("#ios .p").text("-没有更多-");
                }
            }
        });

        $('a[data-toggle="tab"]').on('click', function (e) {
// 获取已激活的标签页的名称
            var activeTab = $(this).attr("href");
            alert(activeTab);
            if(activeTab==".home"&&homeInfo==null){
                Popularize.getData(1, 1);
            }else if(activeTab==".ios"&&iosInfo==null){
                Popularize.getData(2, 1);
            }
        });
    },
    getData: function (type, pageNo) {
        // $(".p").text("正在加载");
        new AjaxRequest({
            type: "post",
            async: true,
            url: apiService.mySpreadAwardList + "?type=" + type,
            param: JSON.stringify({pageNo: pageNo, limit: 10}),
            callBack: function (res) {
                alert(res.payload.items.length);
                if (res.code == 302) {
                    window.location.href = res.url;
                } else if (res.success) {
                    var data = res.payload.items;
                    if (type == 1) {
                        $(".homeamt").text(res.payload.awardAmt);
                        homeInfo = data;
                        homeInfo.pageNo = pageNo;
                        if (data.length==0||data.length<10) {
                            $("#home .p").text("-没有更多-");
                        } else {
                            $("#home .p").text("-加载更多-");
                        }
                        for (let i = 0; i < data.length; i++) {
                            buildDom(data[i]);
                        }
                    } else {
                        $(".iosamt").text(res.payload.awardAmt);
                        iosInfo = data;
                        iosInfo.pageNo = pageNo;
                        if (data.length<10) {
                            $("#ios .p").text("-没有更多-");
                        } else {
                            $("#ios .p").text("-加载更多-");
                        }
                        for (let i = 0; i < data.length; i++) {
                            buildDom1(data[i]);
                        }
                    }
                } else {
                    alert(res.message);
                }
            },
            failBack: function () {
            }
        });
    }
}

function buildDom(data) {
    var imgUrl = "";
    if (data.awardItemType == 1) {
        imgUrl = "../../img/popullarize/gyshare.png";
    } else if (data.awardItemType == 2) {
        imgUrl = "../../img/popullarize/gdshare.png";
    }
    var dom = `<div class="jl-item flexs-pace-between">
                    <div class="flexs-center jl-l">
                        <img src="${imgUrl}"/>
                        <div style="margin-left:0.5rem">
                            <p>获得时间 : ${data.awardDate}</p>
                            <p>用户昵称 : ${data.nickname}</p>
                        </div>
                    </div>
                    <div class="flexs-center jl-r">
                        <p>+${data.awardAmount}元</p>
                    </div>
                </div>`;
    $("#home .c").append(dom);
}

function buildDom1(data) {
    var imgUrl = "";
    if (data.withdrawType == 1) {
        imgUrl = "../../img/popullarize/weixinpay.png";
    } else if (data.withdrawType == 2) {
        imgUrl = "../../img/popullarize/xxpay.png";
    }
    var dom = `<div class="jl-item flexs-pace-between">
                    <div class="flexs-center jl-l">
                        <img src="${imgUrl}"/>
                        <div style="margin-left:0.5rem">
                            <p>交易编号 : ${data.tradeNo}</p>
                            <p>提现时间 : ${data.withdrawDate}</p>
                        </div>
                    </div>
                    <div class="flexs-center jl-r">
                        <div class="jl-r-tx">
                            <div class="flexs-center"><span class="tagimg"></span><span>${data.statusDesc}</span></div>
                            <p>${data.awardAmount}元</p>
                        </div>
                    </div>
                </div>`;
    $("#ios .c").append(dom);
}